package cg.studio.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * VocabularyTree is a RandomTree.
 * But the parent node could keep Vocabulary too. for example "apple" and "apples" are both Vocabulary
 * 
 * @author brightchen
 *
 */
public class VocabularyTree extends RandomTree< Character, String >
{
  private boolean isVocabulary;
  
  public VocabularyTree( String vocabulary )
  {
    this( vocabulary, false );
  }
  
  public VocabularyTree( String vocabulary, boolean isVocabulary )
  {
    super( vocabulary );
    this.isVocabulary = isVocabulary;
  }

  /**
   * 
   * @param list output of the list of leaf values
   * @return
   */
  public List< String > getAllVocabularies()
  {
    List< String > vocabularies = new ArrayList< String >();
    ConcurrentLinkedQueue< VocabularyTree > parentQueue = new ConcurrentLinkedQueue< VocabularyTree >();
    parentQueue.add( this );
    
    while( true )
    {
      VocabularyTree tree = parentQueue.poll();
      if( tree == null )    //reach end
        break;
      if( tree.isVocabulary )
      {
        vocabularies.add( tree.getValue() );
      }
      
      if( !tree.isLeaf() )
      {
        //this is parent, get it's sub
        parentQueue.addAll( tree.listSubTrees() );
      }
    }
    return list;
  }

}
